<template>
  <div>
    <!-- 选择标签 -->
    <el-select
      v-model="value"
      multiple
      filterable
      @change="handleChange"
      placeholder="请选择"
      style="width: 100%"
    >
      <el-option
        v-for="item in options"
        :key="item.id"
        :label="item.name"
        :value="item.id"
      >
      </el-option>
    </el-select>
  </div>
</template>
<script>
import { tbLabelQuery } from '@/api/admin/system/label'
export default {
  data() {
    return {
      value: [],
      options: [],
      listQuery: {
        page: 1,
        limit: 9999,
        type: this.type,
        labelType: this.labelType,
      },
    }
  },
  props: ['defaultValue', 'type', 'labelType'],
  watch: {
    defaultValue(v) {
      this.setValue()
    },
    type(v) {
      this.getList()
    },
  },
  mounted() {
    this.setValue()
    this.getList()
  },
  methods: {
    getList() {
      this.listQuery.type = this.type
      tbLabelQuery(this.listQuery).then((res) => {
        this.options = res.data.data
      })
    },
    setValue() {
      if (!this.defaultValue) {
        this.value = []
        return
      }
      if (!Array.isArray(this.defaultValue)) {
        this.value = this.defaultValue.split(',')
      }
    },
    handleChange(value) {
      this.$emit('e-labelData', value)
    },
  },
}
</script>